<?php
/**
 * 设施点列表
 * User: ttl
 * Date: 2019/5/20
 * Time: 17:23
 */

namespace app\admin\controller;

use app\common\logic\BuildsLogic;
use think\AjaxPage;
use think\Db;
use think\Loader;
use think\Variable;

class Builds extends Base
{
    /*
     * 设施点列表
     */
    public function build_index()
    {

//        $b_logic = new BuildsLogic();
//        $confs = $b_logic->get_build_config();
//        $this->assign('build_types',$confs['build_types']);
        $build_types = Db::name('BuildType')->cache(true, 3600)->select();
        $this->assign('build_types', $build_types);
        return $this->fetch();
    }

    /***
     * 刷新数据
     * @return mixed
     */
    public function ajax_build_index()
    {
        $where = []; // 搜索条件

        // 关键词搜索
        $key_word = input('key_word') ? trim(input('key_word')) : '';
        if ($key_word) {
            $where['names'] = ['like', '%' . $key_word . '%'];
        }

        $type_id = input('build_type/d', 0);
        if ($type_id > 0) {
            $where['build_type'] = $type_id;
        }

        $state = input('state/d', 0);
        if ($state > 0) {
            $where['state'] = $state;
        }

        $count = M('Builds')->where($where)->count();
        $Page = new AjaxPage($count, 20);

        $show = $Page->show();
        $orderby1 = input('orderby1/s', '');
        $orderby2 = input('orderby2/s', '');

        $order_str = "$orderby1 $orderby2";
        $builds = M('Builds')->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $b_logic = new BuildsLogic();
        $confs = $b_logic->get_build_config();

        $build_types = convert_arr_key($confs['build_types'], 'id');
        $regions = convert_arr_key($confs['regions'], 'id');

        $this->assign('build_types', $build_types);
        $this->assign('regions', $regions);
        $this->assign('builds', $builds);


        $this->assign('page', $show);// 赋值分页输出
        return $this->fetch();
    }

    /*
     * 设施点
     */
    public function edit_build()
    {
        $build_id = input('id/d', 0);
        $type = $build_id > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新

        $b_logic = new BuildsLogic();
        $confs = $b_logic->get_build_config();

        //ajax提交验证
        if ((input('is_ajax') == 1) && IS_POST) {

            $data = input('post.');
            $validate = Loader::validate('AddBuilds');
            $return_url = U('admin/Builds/build_index');
            if (!$validate->check($data)) {
//                return ['status'=>'-1','msg'=>$validate->getError(),'result'=>[]];
                $return_arr = array(
                    'msg' => $validate->getError(),
                    'status' => -1,
                    'data' => array('url' => $return_url)
                );

                $this->ajaxReturn($return_arr);
            }

            $new_data['names'] = $data['names'];
            $new_data['icon'] = $data['icon'];
            $new_data['build_type'] = $data['build_type'];
            $new_data['remark'] = $data['remark'];
            $new_data['contact_name'] = $data['contact_name'];
            $new_data['contact_phone'] = $data['contact_phone'];
            $new_data['desc'] = $data['desc'];
            $new_data['region_id'] = $data['region_id'];
            $new_data['address'] = $data['address'];
            $new_data['longitude'] = $data['longitude'];
            $new_data['latitude'] = $data['latitude'];
            $new_data['big_type'] = input('big_type/d',1);
            $builds_images = input('builds_images/a', []);
//            var_dump($builds_images);

            if ($build_id > 0) {
                Db::name('Builds')->where(['id' => $build_id])->update($new_data);
                Db::name('BuildsImg')->where(['build_id' => $build_id])->delete();
            } else {
                $build_id = Db::name('Builds')->insertGetId($new_data);
            }

            if ($build_id) {
                $len = 0;
                foreach ($builds_images as $key => $value) {
                    if (!$value)
                        continue;

                    Db::name('BuildsImg')->insert(['build_id' => $build_id, 'urls' => $value]);
                    $len = $len + 1;
                    if ($len >= 10) {
                        break;
                    }
                }
            }

            $return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => $return_url),
            );
            $this->ajaxReturn($return_arr);
        } else {

        }

        $build_img = Db::name('BuildsImg')->where(['build_id' => $build_id])->select();
        $builds = M('Builds')->where(['id' => $build_id])->find();
        $this->assign('build_types', $confs['build_types']);
        $this->assign('regions', $confs['regions']);
        $this->assign('big_types', $confs['big_types']);
        $this->assign('builds', $builds);
        $this->assign('buildsImages', $build_img);

        return $this->fetch();

    }

    /*
     * 删除建筑点
     */
    public function del_build()
    {
        $ids = I('post.ids', '');
        empty($ids) && $this->ajaxReturn(['status' => -1, 'msg' => "非法操作！", 'data' => '']);
        $builds_ids = rtrim($ids, ",");

        // 删除此设施
        M("Builds")->whereIn('id', $builds_ids)->delete();  //设施表
        M("BuildsImg")->whereIn('build_id', $builds_ids)->delete();  // 设施图片表
        M("Comment")->whereIn('build_id', $builds_ids)->delete();  //设施评论

        $this->ajaxReturn(['status' => 1, 'msg' => '操作成功', 'url' => U("Admin/Builds/build_index")]);
    }

    public function build_type_index()
    {
        return $this->fetch();
    }

    public function ajax_build_type()
    {

        $where = []; // 搜索条件

        // 关键词搜索
        $key_word = input('key_word') ? trim(input('key_word')) : '';
        if ($key_word) {
            $where['type_name'] = ['like', '%' . $key_word . '%'];
        }

        $count = M('BuildType')->where($where)->count();
        $Page = new AjaxPage($count, 20);

        $show = $Page->show();
        $orderby1 = input('orderby1/s', '');
        $orderby2 = input('orderby2/s', '');

        $order_str = "$orderby1 $orderby2";
        $builds_type = M('BuildType')->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('build_types', $builds_type);
        $this->assign('page', $show);// 赋值分页输出
        return $this->fetch();
    }

    public function edit_build_type()
    {

        $build_type_id = input('id/d', 0);

        //ajax提交验证
        if ((input('is_ajax') == 1) && IS_POST) {

            $data = input('post.');
            $validate = Loader::validate('EditBuildsType');
            $return_url = U('admin/Builds/build_type_index');
            if (!$validate->check($data)) {
                $return_arr = array(
                    'msg' => $validate->getError(),
                    'status' => -1,
                    'data' => array('url' => $return_url)
                );

                $this->ajaxReturn($return_arr);
            }

            $new_data['type_name'] = $data['type_name'];


            if ($build_type_id > 0) {
                Db::name('BuildType')->where(['id' => $build_type_id])->update($new_data);
            } else {
                $build_type_id = Db::name('BuildType')->insertGetId($new_data);
            }

            $return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => $return_url),
            );
            $this->ajaxReturn($return_arr);

        }
        $builds_type = Db::name('BuildType')->where(['id' => $build_type_id])->find();
        $this->assign('build_type', $builds_type);
        return $this->fetch();
    }

    public function del_build_type()
    {

        $ids = I('post.ids', '');
        empty($ids) && $this->ajaxReturn(['status' => -1, 'msg' => "非法操作！", 'data' => '']);
        $builds_type_ids = rtrim($ids, ",");

        $builds = M("Builds")->whereIn('build_type', $builds_type_ids)->select();  //设施表

        if(count($builds)>0) {
            $this->ajaxReturn(['status' => -1, 'msg' => "已经有设施绑定了该类型,你先删除对应的设施！", 'data' => '']);
        }

        // 删除此设施
        M("BuildType")->whereIn('id', $ids)->delete();  //设施表

        $this->ajaxReturn(['status' => 1, 'msg' => '操作成功', 'url' => U("Admin/Builds/build_type_index")]);

    }

    public function region_index(){
        return $this->fetch();
    }

    public function ajax_region_index(){
        $where = []; // 搜索条件

        // 关键词搜索
        $key_word = input('key_word') ? trim(input('key_word')) : '';
        if ($key_word) {
            $where['region'] = ['like', '%' . $key_word . '%'];
        }

        $count = M('Region')->where($where)->count();
        $Page = new AjaxPage($count, 20);

        $show = $Page->show();
        $orderby1 = input('orderby1/s', '');
        $orderby2 = input('orderby2/s', '');

        $order_str = "$orderby1 $orderby2";
        $regions = M('Region')->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('regions', $regions);
        $this->assign('page', $show);// 赋值分页输出
        return $this->fetch();
    }

    public function edit_region(){

        $region_id = input('id/d', 0);

        //ajax提交验证
        if ((input('is_ajax') == 1) && IS_POST) {

            $data = input('post.');
            $validate = Loader::validate('EditRegion');
            $return_url = U('admin/Builds/region_index');
            if (!$validate->check($data)) {
                $return_arr = array(
                    'msg' => $validate->getError(),
                    'status' => -1,
                    'data' => array('url' => $return_url)
                );

                $this->ajaxReturn($return_arr);
            }

            $new_data['region'] = $data['region'];


            if ($region_id > 0) {
                Db::name('Region')->where(['id' => $region_id])->update($new_data);
            } else {
                $region_id = Db::name('Region')->insertGetId($new_data);
            }

            $return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => $return_url),
            );
            $this->ajaxReturn($return_arr);

        }
        $region = Db::name('Region')->where(['id' => $region_id])->find();
        $this->assign('region', $region);
        return $this->fetch();

    }

    public function del_region(){
        $ids = I('post.ids', '');
        empty($ids) && $this->ajaxReturn(['status' => -1, 'msg' => "非法操作！", 'data' => '']);
        $region_ids = rtrim($ids, ",");

        $builds = M("Builds")->whereIn('region_id', $region_ids)->select();  //设施表

        if(count($builds)>0) {
            $this->ajaxReturn(['status' => -1, 'msg' => "已经有设施绑定了该类型,你先删除对应的设施！", 'data' => '']);
        }

        // 删除此设施
        M("Region")->whereIn('id', $ids)->delete();  //设施表

        $this->ajaxReturn(['status' => 1, 'msg' => '操作成功', 'url' => U("Admin/Builds/region_index")]);
    }
}